package com.weiyuwqj.server.service.sys.impl;

import com.github.pagehelper.PageHelper;
//import com.weiyuwqj.api.hengfeng.model.base.HfBaseResponseBean;
//import com.weiyuwqj.api.hengfeng.model.base.HfDataBean;
//import com.weiyuwqj.api.hengfeng.model.base.HfResultBean;
//import com.weiyuwqj.api.hengfeng.model.base.HfSuccessDataBean;
//import com.weiyuwqj.api.hengfeng.param.vo.HfCreateEnterpriseVo;
//import com.weiyuwqj.server.pay.MemberApi;
import com.weiyuwqj.server.service.base.BaseServiceImpl;
import com.weiyuwqj.server.mapper.*;
import com.weiyuwqj.api.service.SysUserService;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.model.*;
import com.weiyuwqj.api.model.vo.SysUserVo;
import com.weiyuwqj.api.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lips on 2016/2/3.
 */
@Service("sysUserService")
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService {

    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysUserRoleMapper userRoleMapper;
    @Autowired
    BusAccountMapper accountMapper;
    @Autowired
    BasUserSecurityMapper userSecurityMapper;
    @Autowired
    BusAccountBankMapper accountBankMapper;
    @Autowired
    BusGuaranteeSecurityMapper guaranteeSecurityMapper;
//    @Autowired
//    MemberApi memberApi;
    @Autowired
    BasUserMapper userMapper;

    @Override
    public SysUser doLogin(String username, String password) {
        SysUser user = sysUserMapper.getOneByUsername(username);
        if (user == null) {
            return null;
        }
        try {
            password = MD5.toMD5(password + user.getSalt());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("username", username);
        map.put("password", password);
        user = sysUserMapper.getOneByUsernameAndPassword(map);
        return user;
    }

    //后台列表接口
    @Override
    public PageList getList(Paginator p, Map map) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        List<SysUser> list = sysUserMapper.getList(map);
        return new PageList(list);
    }

    //后台新增
    @Override
    public void insertUser(SysUser user, Integer character) {
        sysUserMapper.insertUser(user);
        int userId = user.getId();
        if (character != null && character > 0) {
            SysUserRole role = new SysUserRole();
            role.setRoleId(character);
            role.setUserId(userId);
            userRoleMapper.insert(role);
        }
        if (user.getType() == 2) {
            BusAccount account = new BusAccount();
            account.setUserId(userId);
            account.setFrozen(BigDecimal.ZERO);
            account.setUsable(BigDecimal.ZERO);
            account.setTotal(BigDecimal.ZERO);
            account.setRepay(BigDecimal.ZERO);
            account.setWait(BigDecimal.ZERO);
            account.setCash(BigDecimal.ZERO);
            accountMapper.insert(account);
        }
    }

    //查询不分页的列表
    @Override
    public List<SysUser> getList(Map map) {
        return sysUserMapper.getList(map);
    }

    @Override
    public SysUserVo getOneByUsername(String username) {
        return sysUserMapper.getOneByUsername(username);
    }

    @Override
    public SysUser getUserByMobile(String mobile) {
        return sysUserMapper.getUserByMobile(mobile);
    }

//    //公司认证
//    @Override
//    public Map guaranteeCertification(BusGuaranteeSecurity security) {
//        //根据userId查看信息
//        BasUser user = userMapper.selectByPrimaryKey(security.getUserId());
//        HfCreateEnterpriseVo enterpriseVo = new HfCreateEnterpriseVo();
//        //公司信息
//        enterpriseVo.setBusinessLicense(security.getBusinessLicense());
//        enterpriseVo.setEnterpriseFullName(security.getEnterpriseFullName());
//        enterpriseVo.setIdentifyCard(security.getLegalNo());
//        enterpriseVo.setAgentPersonPhone(user.getMobile());
//	    HfResultBean<HfDataBean> resultBean = memberApi.createEnterprise(enterpriseVo); // 请求恒丰
//
//	    guaranteeSecurityMapper.insert(security);
//	    BasUserSecurity userSecurity = userSecurityMapper.getOneByUserId(security.getUserId());
//	    if (null == userSecurity) {
//		    userSecurity = new BasUserSecurity();
//		    userSecurity.setRealname(security.getLegalName());
//		    userSecurity.setIdentifyCard(security.getLegalNo());
//		    userSecurity.setRealnameStatus(1);
//		    userSecurity.setUserId(security.getUserId());
//		    userSecurity.setPhoneStatus(1);
//		    userSecurity.setEmailStatus(0);
//		    userSecurityMapper.insert(userSecurity);
//	    } else {
//		    userSecurity.setRealname(security.getLegalName());
//		    userSecurity.setIdentifyCard(security.getLegalNo());
//		    userSecurity.setRealnameStatus(1);
//		    userSecurityMapper.updateByPrimaryKey(userSecurity);
//	    }
//	    //修改用户平台客户号
//	    for (HfSuccessDataBean hengFengSuccessDataBean : resultBean.getSuccessData()) {
//		    userMapper.mobilePlatformId(hengFengSuccessDataBean.getPlatcust(), user.getId());
//	    }
//	    return Message.OK;
//    }


//    @Override
//    public Map updateEnterprise(BusGuaranteeSecurity security, BusAccountBank bank, String newBankCardNo, String bankCardName, String bizType) {
//        if (security != null) {
//	        BusGuaranteeSecurity exsitSecurity = guaranteeSecurityMapper.getOneByUserId(security.getUserId());
//	        security.setId(exsitSecurity.getId());
//        } else {
//            return Message.FAIL;
//        }
//        HfCreateEnterpriseVo enterpriseVo = new HfCreateEnterpriseVo();
//        //公司信息
//        enterpriseVo.setUserId(security.getUserId() + "");
//        enterpriseVo.setAgentPersonPhone(security.getAgentPersonPhone());
//        enterpriseVo.setBusinessLicense(security.getBusinessLicense());
//        enterpriseVo.setEnterpriseFullName(security.getEnterpriseFullName());
//        //银行卡信息
//        enterpriseVo.setBankName(bank.getBankName());
//        enterpriseVo.setBankCity(bank.getCity());
//        enterpriseVo.setBankCardNo(bank.getCardNo());
//        enterpriseVo.setBankCode(bank.getBankCode());
//        enterpriseVo.setBankProvince(bank.getProvince());
//        //enterpriseVo.setCallbackUrl(Global.CONFIG.get("company_certification_callback_url"));
//        enterpriseVo.setIssuerName(bank.getSubBranch());
//        enterpriseVo.setBankCardName(bankCardName);
//        enterpriseVo.setIssuer(security.getIssuer());
//        enterpriseVo.setBizType(bizType);
//        enterpriseVo.setNewBankCardNo(newBankCardNo);
//
//        Map map = memberApi.UpdateEnterprise(enterpriseVo);
//        BusAccountBank accountBank = accountBankMapper.getOneByUserId(security.getUserId());
//        if (map.get("respCode").equals("00")) {
//            security.setCertType("BLC");
//            security.setRegionCode("086");
//            guaranteeSecurityMapper.updateByPrimaryKey(security);
//            bank.setStatus(1);
//            bank.setMobile(security.getAgentPersonPhone());
//            bank.setType(3);
//            bank.setAddtime(new Date());
//            BasUserSecurity userSecurity = userSecurityMapper.getOneByUserId(security.getUserId());
//            userSecurity.setRealname(security.getLegalName());
//            userSecurity.setIdentifyCard(security.getLegalNo());
//            userSecurity.setRealnameStatus(1);
//            userSecurityMapper.updateByPrimaryKey(userSecurity);
//            if (accountBank == null) {
//                accountBankMapper.insert(bank);
//            } else {
//                bank.setId(accountBank.getId());
//                accountBankMapper.updateByPrimaryKeySelective(bank);
//            }
//
//            return Message.OK;
//        } else {
//            map.put("result", "fail");
//            map.put("messageText", map.get("respMsg"));
//            return map;
//        }
//    }



}
